<?php
require_once 'common/config.php';
require_once 'common/functions.php';

// 检查登录状态
check_login();

$db = db_connect();

// 获取搜索和筛选参数
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$status = isset($_GET['status']) ? intval($_GET['status']) : -1; // 默认状态为-1，表示全部
$course_id = isset($_GET['course_id']) ? intval($_GET['course_id']) : 0;
$date = isset($_GET['date']) ? trim($_GET['date']) : '';

// 每页显示的记录数
$per_page = 10;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = ($page - 1) * $per_page;

// 构建查询条件
$where = "b.type = 2"; // 只查询团课预约
$params = [];

if ($search) {
    $where .= " AND (u.name LIKE :search OR u.phone LIKE :search OR c.name LIKE :search)";
    $params[':search'] = "%{$search}%";
}

if ($status != -1) {
    $where .= " AND b.status = :status"; // 只在状态不为-1时添加条件
    $params[':status'] = $status;
}

if ($course_id) {
    $where .= " AND b.course_id = :course_id";
    $params[':course_id'] = $course_id;
}

if ($date) {
    $where .= " AND b.booking_date = :date";
    $params[':date'] = $date;
}

// 获取总记录数
$count_sql = "SELECT COUNT(*) FROM bookings b 
              LEFT JOIN users u ON b.user_id = u.id 
              LEFT JOIN courses c ON b.course_id = c.id 
              WHERE {$where}";
$count_stmt = $db->prepare($count_sql);
foreach ($params as $key => $value) {
    $count_stmt->bindValue($key, $value);
}
$count_stmt->execute();
$total_records = $count_stmt->fetchColumn();
$total_pages = ceil($total_records / $per_page);

// 获取预约记录
$sql = "SELECT b.*, c.name as course_name, u.name as user_name 
        FROM bookings b 
        LEFT JOIN courses c ON b.course_id = c.id 
        LEFT JOIN users u ON b.user_id = u.id 
        WHERE {$where} 
        ORDER BY b.created_at DESC 
        LIMIT {$offset}, {$per_page}";

$stmt = $db->prepare($sql);
foreach ($params as $key => $value) {
    $stmt->bindValue($key, $value);
}
$stmt->execute();
$bookings = $stmt->fetchAll(PDO::FETCH_ASSOC);

// 状态映射
$status_types = [
    0 => ['name' => '待确认', 'badge' => 'badge-warning'],
    1 => ['name' => '已确认', 'badge' => 'badge-info'],
    2 => ['name' => '已完成', 'badge' => 'badge-success'],
    3 => ['name' => '已取消', 'badge' => 'badge-danger']
];
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>团课预约管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        .filter-bar {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
            margin-bottom: 20px;
        }

        .search-bar {
            display: flex;
            gap: 15px;
            align-items: center;
            flex-wrap: wrap;
        }

        .search-bar input[type="text"],
        .search-bar select,
        .search-bar input[type="date"] {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            transition: border-color 0.3s;
            background: #f8f9fa;
        }

        .search-bar input[type="text"]:focus,
        .search-bar select:focus,
        .search-bar input[type="date"]:focus {
            outline: none;
            border-color: #3498db;
            background: #fff;
        }

        .search-bar button {
            padding: 10px 15px;
            background: #3498db;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background 0.3s;
        }

        .search-bar button:hover {
            background: #2980b9;
        }

        .data-table {
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
            overflow: hidden;
        }

        .data-table table {
            width: 100%;
            border-collapse: collapse;
        }

        .data-table th {
            background: #f8f9fa;
            padding: 12px 15px;
            text-align: left;
            font-weight: 600;
            color: #2c3e50;
            border-bottom: 2px solid #eee;
        }

        .data-table td {
            padding: 12px 15px;
            border-bottom: 1px solid #eee;
            vertical-align: middle;
        }

        .data-table tr:hover {
            background: #f8f9fa;
        }

        .badge {
            padding: 5px 10px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: 500;
        }

        .badge-warning {
            background: #fff3cd;
            color: #856404;
            border: 1px solid #ffeeba;
        }

        .badge-info {
            background: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }

        .badge-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .badge-danger {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <?php include 'includes/sidebar.php'; ?>
        
        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>预约记录管理</h2>
                </div>
            </header>

            <div class="content">
                <!-- 搜索和筛选 -->
                <div class="filter-bar">
                    <form action="" method="GET" id="searchForm" class="search-bar">
                        <input type="text" name="search" placeholder="搜索用户名、手机号、课程名..." 
                               value="<?php echo htmlspecialchars($search); ?>">
                        <select name="status" onchange="document.getElementById('searchForm').submit()">
                            <option value="-1">全部状态</option>
                            <?php foreach ($status_types as $key => $value): ?>
                            <option value="<?php echo $key; ?>" <?php echo $status === $key ? 'selected' : ''; ?>>
                                <?php echo $value['name']; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                        <input type="date" name="date" value="<?php echo $date; ?>" 
                               onchange="document.getElementById('searchForm').submit()">
                        <button type="submit"><i class="fas fa-search"></i></button>
                    </form>
                </div>

                <!-- 数据表格 -->
                <div class="data-table">
                    <table>
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>用户</th>
                                <th>课程名称</th>
                                <th>预约时间</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($bookings as $booking): ?>
                            <tr>
                                <td><?php echo $booking['id']; ?></td>
                                <td><?php echo htmlspecialchars($booking['user_name']); ?></td>
                                <td><?php echo htmlspecialchars($booking['course_name']); ?></td>
                                <td><?php echo $booking['booking_date']; ?><br><?php echo htmlspecialchars($booking['time_slot']); ?></td>
                                <td>
                                    <span class="badge <?php echo $status_types[$booking['status']]['badge']; ?>">
                                        <?php echo $status_types[$booking['status']]['name']; ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="booking_details.php?id=<?php echo $booking['id']; ?>" 
                                       class="btn-icon" title="查看详情">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <!-- 添加分页 -->
                <?php 
                echo generate_pagination($page, $total_pages, [
                    'search' => $search,
                    'status' => $status
                ]); 
                ?>
            </div>
        </div>
    </div>
</body>
</html> 